package org.example.main.service.impl;

import org.apache.log4j.Logger;
import org.example.main.dao.LandlordMapper;
import org.example.main.entity.Landlord;
import org.example.main.service.LandlordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LandlordServiceImpl implements LandlordService {

    private static final Logger logger = Logger.getLogger(LandlordServiceImpl.class);

    @Autowired
    private LandlordMapper landlordMapper;

    @Override
    public int add(Landlord landlord) {
        logger.info("Adding new landlord with username: " + landlord.getUsername());
        int result = landlordMapper.insert(landlord);
        logger.info("Landlord added with ID: " + landlord.getLandlordId() + ", result: " + result);
        return result;
    }

    @Override
    public int deleteById(long landlordId) {
        logger.info("Deleting landlord by ID: " + landlordId);
        int result = landlordMapper.deleteById(landlordId);
        logger.info("Landlord deletion result: " + result);
        return result;
    }

    @Override
    public int updateById(Landlord landlord) {
        logger.info("Updating landlord with ID: " + landlord.getLandlordId());
        int result = landlordMapper.updateById(landlord);
        logger.info("Landlord update result: " + result);
        return result;
    }

    @Override
    public Landlord findById(long landlordId) {
        logger.info("Finding landlord by ID: " + landlordId);
        Landlord landlord = landlordMapper.findById(landlordId);
        logger.debug("Found landlord: " + landlord);
        return landlord;
    }

    @Override
    public Landlord findByUsername(String username) {
        logger.info("Finding landlord by username: " + username);
        Landlord landlord = landlordMapper.findByUsername(username);
        logger.debug("Found landlord: " + landlord);
        return landlord;
    }

    @Override
    public List<Landlord> findAll() {
        logger.info("Retrieving all landlords");
        List<Landlord> landlords = landlordMapper.findAll();
        logger.debug("Found " + landlords.size() + " landlords");
        return landlords;
    }
}
